#!/usr/bin/env coffee
import {walkRel} from '@rmw/walk'
import {homedir} from 'os'
import * as CMD from '../src/const/cmd'
import {encode as utf8Encode} from '@rmw/utf8'
import sleep from 'await-sleep'

root = "#{homedir()}/.rmw/mnt"

import dgram from 'dgram'

ip = "61.49.250.90"
port = 64267

client = dgram.createSocket('udp4')
client.on 'message', (msg, {address,family,port,size})=>
  console.log msg, address, port

udp_send = (msg)=>
  client.send(
    msg
    port
    ip
    (err)=>
      console.log err
      console.log client.address()
  )

send = udp_send

do =>
  for await file from walkRel(root)
    name = utf8Encode(file)
    send new Uint8Array [
      CMD.TIME_SIZE_HASH
      ...name
    ]


###
import Nkn from '../src/nkn/init.coffee'
import sleep from 'await-sleep'

nkn = await Nkn()

to = "95c87839826a348bf2717f9eec0dacd8c6567e5a0f545c2b88df74a3bb7fcbf3"

file = "rmw/package.json"



send = 0
  console.log ++send, file
  # console.log file.length, name.length
  try
    r = await nkn.send(
      to
      #noReply:true
    )
  catch err
    console.error err
    continue

  view = new DataView r[...16].buffer
  size = view.getBigUint64 8,true
  hash = r[16...48]

  bin = new Uint8Array 1+32+8
  bin[0] = CMD.DATA
  bin.set hash,1
  view = new DataView bin.buffer
  view.setBigUint64 33, 0n,true

  console.log await nkn.send to, bin

process.exit()
###
